package com.lanou.dllo.baisibudejie.adapater;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.dllo.baisibudejie.R;
import com.lanou.dllo.baisibudejie.db.entity.Sql_MyNewsEntity;
import com.lanou.dllo.baisibudejie.view.CircleImage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/23.
 */
public class MyNewsRecycleAdapter extends RecyclerView.Adapter<MyNewsRecycleAdapter.JokesViewHolder> {

    private Context context;
    private List<Sql_MyNewsEntity> datas;
    private RequestQueue queue;

    public MyNewsRecycleAdapter(Context c) {
        this.context = c;
        queue = Volley.newRequestQueue(c);


    }

    /**
     * 加载行布局
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public JokesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.my_news_recycle_item, null);

        return new JokesViewHolder(view);
    }

    /**
     * 解析数据设置内容
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(JokesViewHolder holder, int position) {
        List<Sql_MyNewsEntity> entities = datas;
        //for (Sql_MyNewsEntity entity : entities) {
            holder.my_news_nameTv.setText(entities.get(position).getName());
            Log.i("lanou", "----------------------" + entities.get(position).getName() + entities.get(position).getImageUrl());
            String url = entities.get(position).getImageUrl();
            getBitmap(url, holder.my_news_headIv);
        //}


    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    /**
     * 组件缓存内部类
     */
    class JokesViewHolder extends RecyclerView.ViewHolder {


        private TextView my_news_nameTv;
        private ImageView my_news_headIv;//头像


        public JokesViewHolder(View v) {
            super(v);
            my_news_nameTv = (TextView) v.findViewById(R.id.my_news_nameTv);
            my_news_headIv = (ImageView) v.findViewById(R.id.my_news_headIv);
        }

    }

    public void addData(List<Sql_MyNewsEntity> data) {
        this.datas = data;
        notifyDataSetChanged();//数据改变 页面刷新
    }

    private void getBitmap(String url, final ImageView imageView) {

        ImageRequest request = new ImageRequest(url,
                new Response.Listener<Bitmap>() {

                    @Override
                    public void onResponse(Bitmap response) {
                        int radius;
                        // 将返回的bitmap图片对象设置到图片组件中
                        if (response.getWidth()<=response.getHeight()){
                            radius = response.getWidth()/2;
                        }else {
                            radius = response.getHeight()/2;
                        }

                        Bitmap circleImage = new CircleImage(context).getCroppedRoundBitmap(response, radius);
                        imageView.setImageBitmap(circleImage);
                    }
                },
                0, 0, Bitmap.Config.ARGB_8888,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                        error.printStackTrace();
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // TODO Auto-generated method stub
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        queue.add(request);//将本次图片请求加入到请求队列中
    }
}
